package com.kcwl.carrier.application.service;

import com.kcwl.carrier.application.command.CarrierBlacklistCommand;
import com.kcwl.carrier.domain.entity.CarrierBlacklist;
import com.kcwl.carrier.domain.entity.CarrierInfo;
import com.kcwl.carrier.domain.service.CarrierBlacklistDomainService;
import com.kcwl.carrier.domain.condition.CarrierBlacklistPageCondition;
import com.kcwl.carrier.domain.service.CarrierInfoDomainService;
import com.kcwl.carrier.domain.service.ForeignDriverDomainService;
import com.kcwl.carrier.interfaces.dto.CarrierBlacklistDTO;
import com.kcwl.carrier.interfaces.query.CarrierBlacklistPageQuery;
import com.kcwl.carrier.interfaces.query.UserIdQuery;
import com.kcwl.ddd.domain.entity.KcPage;
import com.kcwl.ddd.infrastructure.utils.KcPageConverter;
import com.kcwl.ddd.interfaces.dto.PageInfoDTO;
import com.kcwl.framework.utils.CollectionUtil;
import com.kcwl.framework.utils.KcBeanConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 刘旭超
 * @date 2022年11月21日 13:54
 * @Description
 */
@Service
public class CarrierBlacklistApplicationService {

    @Autowired
    CarrierBlacklistDomainService carrierBlacklistDomainService;
    @Autowired
    CarrierInfoDomainService carrierInfoDomainService;
    @Autowired
    ForeignDriverDomainService foreignDriverDomainService;

    /**
     * 分页列表查询
     */
    public PageInfoDTO<CarrierBlacklistDTO> queryPageList(CarrierBlacklistPageQuery query) {
        KcPage<CarrierBlacklist> carrierBlacklists = carrierBlacklistDomainService.queryPageList(KcBeanConverter.toBean(query, CarrierBlacklistPageCondition.class));
        return KcPageConverter.toPageInfoDTO(carrierBlacklists, CarrierBlacklistDTO.class);
    }

    /**
     * 添加黑名单
     */
    public void addBlacklist(CarrierBlacklistCommand command) {
        carrierBlacklistDomainService.addBlacklist(KcBeanConverter.toBean(command, CarrierBlacklist.class));
    }

    /**
     * 用户的司机黑名单列表
     *
     * @param query
     * @return
     */
    public PageInfoDTO<CarrierBlacklistDTO> queryPageListByUser(CarrierBlacklistPageQuery query) {
        List<Long> driverIds = new ArrayList<>();
        //获取自由司机
        List<CarrierInfo> carrierInfos = carrierInfoDomainService.getCarrierDrivers(KcBeanConverter.toBean(query, UserIdQuery.class));

        if (!CollectionUtil.isEmpty(carrierInfos)) {
            driverIds.addAll(carrierInfos.stream().map(CarrierInfo::getId).collect(Collectors.toList()));
        }
        //获取外协司机id列表
        List<Long> fDriverIds = foreignDriverDomainService.selectDriverIdsByCarrierId(query.getUserId());
        if (!fDriverIds.isEmpty()) {
            driverIds.addAll(fDriverIds);
        }
        if (driverIds.isEmpty()) {
            return new PageInfoDTO<>(0, query.getCurPagerNo(), query.getPageSize(), driverIds);
        }
        CarrierBlacklistPageCondition condition = KcBeanConverter.toBean(query, CarrierBlacklistPageCondition.class);
        condition.setCarrierIds(driverIds);
        KcPage<CarrierBlacklist> carrierBlacklists = carrierBlacklistDomainService.queryPageList(condition);
        List<CarrierInfo> carrierInfoList = carrierInfoDomainService.getByIds(driverIds);
        PageInfoDTO<CarrierBlacklistDTO> pageInfoDTO = KcPageConverter.toPageInfoDTO(carrierBlacklists, CarrierBlacklistDTO.class);
        List<CarrierBlacklistDTO> carrierBlacklists1 = pageInfoDTO.getList();
        if (carrierBlacklists1.isEmpty()) {
            return pageInfoDTO;
        }
        //后期设置 黑名单详细信息 目前设置准驾车型
        Map<Long, CarrierInfo> carrierInfoMap = carrierInfoList.stream().collect(Collectors.toMap(CarrierInfo::getId, e -> e));
        if (!carrierBlacklists1.isEmpty()) {
            for (CarrierBlacklistDTO carrierBlacklistDTO : carrierBlacklists1) {
                CarrierInfo carrierInfo = carrierInfoMap.get(carrierBlacklistDTO.getCarrierId());
                carrierBlacklistDTO.setAllowDrivingModel(carrierInfo.getAllowDrivingModel());
            }
        }
        return pageInfoDTO;
    }
}
